package com.twu.biblioteca.resources;

import com.twu.biblioteca.models.Book;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: arvind
 * Date: 11/10/13
 * Time: 4:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class BookList {

    private static BookList instance=new BookList();
    private static HashMap<String,Book> bookList;

    private BookList(){loadBooks();}

    public static BookList getInstance()
    {
        return instance;
    }


    private void loadBooks() {

        bookList = new HashMap<String,Book>();

        String[][] bookDetails={{"Head First Java","99921-58-10-7"},
                {"TDD By Example","88921-58-10-7"},
                {"Continuous Integration-cycletime","77921-58-10-7"},
                {"SVN","66921-58-10-7"}};

        for(int i=0;i<bookDetails.length;i++)
        {
            Book book=new Book("B"+(i+1),bookDetails[i][0],bookDetails[i][1]);
            bookList.put(book.getBookID(), book);
        }

    }

    public String getBooksDetail() {

        StringBuffer response=new StringBuffer();

        response.append("Book List\n");

        Set<String> bookIDs=bookList.keySet();
        List<String> keyList=new ArrayList<String>(bookIDs);
        Collections.sort(keyList);

        for(String bookID:keyList) {
            Book book=bookList.get(bookID);
            response.append(book.getBookDetails());
        }
        return response.toString();
    }

    public boolean isBookIDValid(String bookID) {

        Book book = bookList.get(bookID);

        if(book!=null)
            return true;
        else
            return false;

    }
}
